package com.eeepay.eeepay_v2.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.activity.ChooseActivity;
import com.eeepay.eeepay_v2.activity.DeviceTypeActivity;
import com.eeepay.eeepay_v2.activity.MerchantStateActivity;
import com.eeepay.eeepay_v2.activity.PosTypeActivity;
import com.eeepay.eeepay_v2.activity.TrueFalseActivity;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.eeepay_v2.util.TimeUtils;
import com.eeepay.v2_library.log.LogUtils;
import com.eeepay.v2_library.util.ScreenSwitch;
import com.eeepay.v2_library.view.HorizontalItemView;
import com.eeepay.v2_library.view.LabelEditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.grpc.ManagedChannel;

/**
 * Created by zw on 2016/3/4 0004.
 * 进件查询筛选 PopWindow
 */
public class IntoQueryPopView extends PopupWindow implements View.OnClickListener {

    private View view;
    private Context mContext;
    private TextView cancle, reset;//取消、重置
    private TextView createTime, endTime;
    private HorizontalItemView status   //状态
            , device  //设备类型
            , product;//业务产品
    private ImageView iv_hasNext;
    private LabelEditText merId //商户名称或编号
            , phone, agentId;//代理商名称或编号
    private Button confirm;//确定

    private ConfirmListener listener;

    private String statusCode = "-1";
    private boolean hasNext = false;
    private String bp_id = "";

    public IntoQueryPopView(Context context) {
        super(context);
        mContext = context;
        initView();
        setWidget();
    }

    private void initView() {
        view = View.inflate(mContext, R.layout.pop_into_query_view, null);
        this.setInputMethodMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        //设置PopupWindow的View
        this.setContentView(view);
        this.setFocusable(true);
        //设置PopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.popwin_right_anim_style);
        //菜单背景色
        ColorDrawable dw = new ColorDrawable(Constant.GRAY_LUCENCY_COLOR);
        this.setBackgroundDrawable(dw);
        cancle = getViewById(R.id.tv_cancle);
        reset = getViewById(R.id.tv_reset);
        status = getViewById(R.id.tv_state_all);
        createTime = getViewById(R.id.tv_create_time);
        endTime = getViewById(R.id.tv_end_time);
        iv_hasNext = getViewById(R.id.iv_has_next);
        device = getViewById(R.id.tv_device_all);
        merId = getViewById(R.id.tv_nameOrNumber);
        phone = getViewById(R.id.tv_phone);
        product = getViewById(R.id.tv_product);
        agentId = getViewById(R.id.tv_agent_nameOrNumber);
        confirm = getViewById(R.id.btn_confirm);

        iv_hasNext.setSelected(hasNext);
        agentId.setVisibility(View.GONE);

    }

    private void setWidget() {
        cancle.setOnClickListener(this);
        reset.setOnClickListener(this);
        status.setOnClickListener(this);
        product.setOnClickListener(this);
        iv_hasNext.setOnClickListener(this);
        device.setOnClickListener(this);
        confirm.setOnClickListener(this);
        TimeUtils.selectTime(mContext, createTime, TimeUtils.DATE);
        TimeUtils.selectTime(mContext, endTime, TimeUtils.DATE);
    }

    public void showPopWindow(View parent) {
        if (!this.isShowing()) {
            this.showAtLocation(parent, Gravity.RIGHT, 0, 0);
        }
    }

    @Override
    public void dismiss() {
        if (this.isShowing()) {
            super.dismiss();
            WindowManager.LayoutParams params = ((Activity) mContext).getWindow().getAttributes();
            params.alpha = 1f;
            ((Activity) mContext).getWindow().setAttributes(params);
        }
    }

    public <T extends View> T getViewById(int id) {
        View view = this.view.findViewById(id);
        return (T) view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancle:
                this.dismiss();
                break;
            case R.id.tv_reset:
                resetAll();
                break;
            case R.id.tv_product://业务产品
                Bundle bundle = new Bundle();
                bundle.putString(Constant.INTENT_FLAG, Constant.PRODUCT_LIST);
                ScreenSwitch.switchActivity(mContext, ChooseActivity.class, bundle,
                        Constant.RequestCode.product);
                break;
            case R.id.tv_state_all:
                ScreenSwitch.switchActivity(mContext, MerchantStateActivity.class, null,
                        Constant.RequestCode.merchant_state_all);
                break;
            case R.id.iv_has_next:
                hasNext = !hasNext;
                iv_hasNext.setSelected(hasNext);
                agentId.setVisibility(hasNext ? View.VISIBLE : View.GONE );
                break;
            case R.id.tv_device_all:
                Bundle bundle1 = new Bundle();
                bundle1.putBoolean("isSingle", false);
                ScreenSwitch.switchActivity(mContext, PosTypeActivity.class, bundle1,
                        Constant.RequestCode.device_all);
                break;
            case R.id.btn_confirm://确认
                if (listener != null) {
                    //获取当前所有输入的筛选条件值
                    Map<String, String> map = new HashMap<>();
//                    map.put("status", status.getRightText());
                    map.put("status", statusCode);
                    map.put("merId", merId.getEditContent());
                    map.put("phone", phone.getEditContent());
                    if(!"请选择".equals(product.getRightText())){
                        map.put("product", (String) product.getTag());
                    }
                    map.put("createTime", createTime.getText().toString());
                    map.put("endTime", endTime.getText().toString());
                    map.put("agentId", agentId.getEditContent());
                    map.put("hasNext", hasNext ? "1" : "0");
                    map.put("device", bp_id);
                    listener.confirm(map);
                }
                this.dismiss();
                break;
        }
    }



    /**
     * 重置所有选项
     */
    private void resetAll() {
        status.setRightText("全部");
        merId.setEditContent("");
        phone.setEditContent("");
        product.setRightText("请选择");
        createTime.setText("");
        endTime.setText("");
        agentId.setEditContent("");
        iv_hasNext.setSelected(true);
        device.setRightText("全部");
    }

    /**
     * 其他Activity返回的值
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void callBack(int requestCode, int resultCode, Intent data) {
        if (resultCode == ((Activity) mContext).RESULT_OK) {
            if (requestCode == Constant.RequestCode.merchant_state_all) {
                boolean isAll = data.getBooleanExtra("isAll", false);
                if (isAll) {
                    this.status.setRightText("全部");
                    statusCode = "-1";
                } else {
                    String state = data.getStringExtra("state");
                    statusCode = data.getStringExtra("stateCode");
                    this.status.setRightText(state);
                    Log.d("state","state >>> "+state);
                }
            } else if (requestCode == Constant.RequestCode.device_all) {
                boolean isAll = data.getBooleanExtra("isAll", false);
                if (isAll) {
                    this.device.setRightText("全部");
                    bp_id = "";
                } else {
                    String device = data.getStringExtra("device_type");
                    if(TextUtils.isEmpty(device)){
                        this.device.setRightText("全部");
                        bp_id = "";
                    }else {
                        this.device.setRightText(device);
                        bp_id = data.getStringExtra("hp_id");
                    }
                }
            }else if(requestCode == Constant.RequestCode.product){
                String bp_id = data.getStringExtra(Constant.BP_ID);
                String bp_name = data.getStringExtra(Constant.BP_NAME);
                product.setRightText(bp_name);
                product.setTag(bp_id);
                LogUtils.d("TAG", bp_id + bp_name);
            }
        }
    }

    public void setConfirmListener(ConfirmListener listener) {
        this.listener = listener;
    }

    /**
     * 筛选完成时候的监听
     */
    public interface ConfirmListener {
        void confirm(Map<String, String> params);
    }

}
